package com.effectivsocial.rest.controller;
/**
 * 
 */


import java.util.ArrayList;
import java.util.List;
import javax.inject.Named;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Context;

import lombok.extern.slf4j.Slf4j;

import org.apache.cxf.jaxrs.ext.MessageContext;
import org.springframework.beans.BeanUtils;
import org.springframework.social.twitter.api.TwitterProfile;

import com.effectivsocial.rest.service.TwitterBlockOperationsService;
import com.effectivsocial.rest.twitter.domain.SimpleTwitterId;
import com.effectivsocial.rest.twitter.domain.SimpleTwitterProfile;
import com.effectivsocial.rest.util.TwitterFactory;

/**
 * @author Dhrubo
 *
 */
@Named
@Path("v1/twitterservice/blockoperations")
@Slf4j
public class TwitterBlockOperationsController{
	
	
	@POST
	@Path("/blockbyid/{userId}")
	public SimpleTwitterProfile block(@PathParam("userId") long userId, @Context MessageContext context) {
		if(log.isDebugEnabled()){
			log.debug("Blocking user with twitter id - {}", userId);
		}
		
		TwitterBlockOperationsService service = new TwitterBlockOperationsService(TwitterFactory.getTwitter(context));
		TwitterProfile profile = service.block(userId);
		SimpleTwitterProfile simpleProfile = BeanUtils.instantiate(SimpleTwitterProfile.class);
		BeanUtils.copyProperties(profile, simpleProfile);
		
		if(log.isDebugEnabled()){
			log.debug("Twitter profile just blocked = {}", simpleProfile);
		}
		
		return simpleProfile;
	}
	
	
	@POST
	@Path("/block/{screenName}")
	public SimpleTwitterProfile block(@PathParam("screenName") String screenName, @Context MessageContext context) {
		if(log.isDebugEnabled()){
			log.debug("Blocking user with twitter screen name - {}", screenName);
		}
		
		TwitterBlockOperationsService service = new TwitterBlockOperationsService(TwitterFactory.getTwitter(context));
		TwitterProfile profile = service.block(screenName);
		SimpleTwitterProfile simpleProfile = BeanUtils.instantiate(SimpleTwitterProfile.class);
		BeanUtils.copyProperties(profile, simpleProfile);
		
		if(log.isDebugEnabled()){
			log.debug("Twitter profile just blocked = {}", simpleProfile);
		}
		
		return simpleProfile;
	}
	
	//TWITTER - GET blocks/blocking/ids
	@GET
	@Path("/getblockeduserids")
	public List<SimpleTwitterId> getBlockedUserIds(@Context MessageContext context) {
		TwitterBlockOperationsService service = new TwitterBlockOperationsService(TwitterFactory.getTwitter(context));
		List<Long> blockedUserIds = service.getBlockedUserIds();
		
		if(log.isDebugEnabled()){
			log.debug("Blocked user Ids = {}", blockedUserIds);
		}
		
		SimpleTwitterId twitterId = null;
		
		List<SimpleTwitterId> ids = new ArrayList<SimpleTwitterId>();
		
		for(Long id : blockedUserIds){
			twitterId = BeanUtils.instantiate(SimpleTwitterId.class);
			twitterId.setId(id);
			ids.add(twitterId);
		}
		
		return ids;
	}
	
	//TWITTER - GET blocks/blocking
	@GET
	@Path("/getallblockedusers")
	public List<SimpleTwitterProfile> getBlockedUsers(@Context MessageContext context) {
		TwitterBlockOperationsService service = new TwitterBlockOperationsService(TwitterFactory.getTwitter(context));
		List<TwitterProfile> profiles = service.getBlockedUsers();
		
		List<SimpleTwitterProfile> simpleProfiles = new ArrayList<SimpleTwitterProfile>(); 
		
		SimpleTwitterProfile simpleProfile = null;
		
		for(TwitterProfile profile : profiles) {
			simpleProfile = BeanUtils.instantiate(SimpleTwitterProfile.class);
			BeanUtils.copyProperties(profile, simpleProfile);
			simpleProfiles.add(simpleProfile);
		}
				
		return simpleProfiles;
	}
	
	//NOT RELEASED - Pagination not working
	//TWITTER - GET blocks/blocking
	//@GET
	//@Path("/getblockedusers/{page}/{pageSize}")
	public List<SimpleTwitterProfile> getBlockedUsers(@PathParam("page") int page, @PathParam("pageSize") int pageSize, @Context MessageContext context) {
		TwitterBlockOperationsService service = new TwitterBlockOperationsService(TwitterFactory.getTwitter(context));		
		List<TwitterProfile> profiles = service.getBlockedUsers(page,pageSize);
		
		List<SimpleTwitterProfile> simpleProfiles = new ArrayList<SimpleTwitterProfile>(); 
		
		SimpleTwitterProfile simpleProfile = null;
		
		for(TwitterProfile profile : profiles) {
			simpleProfile = BeanUtils.instantiate(SimpleTwitterProfile.class);
			BeanUtils.copyProperties(profile, simpleProfile);
			simpleProfiles.add(simpleProfile);
		}
				
		return simpleProfiles;
	}
	
	//TWITTER - blocks/exists
	@GET
	@Path("/isblockingbyid/{userId}")
	public boolean isBlocking(@PathParam("userId") long userId, @Context MessageContext context) {
		TwitterBlockOperationsService service = new TwitterBlockOperationsService(TwitterFactory.getTwitter(context));		
		return service.isBlocking(userId);
	}
	
	@GET
	@Path("/isblocking/{screenName}")
	public boolean isBlocking(@PathParam("screenName") String screenName, @Context MessageContext context) {
		TwitterBlockOperationsService service = new TwitterBlockOperationsService(TwitterFactory.getTwitter(context));		
		return service.isBlocking(screenName);
	}
	
	@POST
	@Path("/unblockbyid/{userId}")
	public SimpleTwitterProfile unblock(@PathParam("userId") long userId, @Context MessageContext context) {
		TwitterBlockOperationsService service = new TwitterBlockOperationsService(TwitterFactory.getTwitter(context));	
		TwitterProfile profile = service.unblock(userId);
		SimpleTwitterProfile simpleProfile = BeanUtils.instantiate(SimpleTwitterProfile.class);
		BeanUtils.copyProperties(profile, simpleProfile);
		
		if(log.isDebugEnabled()){
			log.debug("Twitter profile just blocked = {}", simpleProfile);
		}
		
		return simpleProfile;
	}
	
	@POST
	@Path("/unblock/{screenName}")
	public SimpleTwitterProfile unblock(@PathParam("screenName") String screenName, @Context MessageContext context) {
		TwitterBlockOperationsService service = new TwitterBlockOperationsService(TwitterFactory.getTwitter(context));		
		TwitterProfile profile = service.unblock(screenName);
		SimpleTwitterProfile simpleProfile = BeanUtils.instantiate(SimpleTwitterProfile.class);
		BeanUtils.copyProperties(profile, simpleProfile);
		
		if(log.isDebugEnabled()){
			log.debug("Twitter profile just blocked = {}", simpleProfile);
		}
		
		return simpleProfile;
	}

}
